<?php

class TransferAuditController extends TController
{
	public $layout='column2';

	/**
	 * @var CActiveRecord the currently loaded data model instance.
	 */
	private $_model;

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to access 'index' and 'view' actions.
				'actions'=>array('index','view'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated users to access all actions
				'users'=>array('@'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}


	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$id = isset($_POST['id'])?(int)$_POST['id']:(int)$_GET['id'];
		$error = 0;
		if($id)
		{
			//审核权限
			$transferModel = new Transfer;
			$data = $transferModel->getTransferDetails($id);
			//判断当前是否是新部门的直属上级 michelle
			$formType = 0;
			foreach ($data['transfer']->process as $key => $value) {
				# code...
				if($value->unique_id == $this->userInfo->unique_id && $value->flag == 1)
				{
					$formType = 1;
					break;
				}
				else if ($value->unique_id == $this->userInfo->unique_id && $value->flag == 2)
				{
					$formType = 2;
					break;
				}
			}

			//p
			if($transferModel->auditCheck($data['transfer'],$this->userInfo->unique_id))
			{
				
				if($_POST)
				{
					//表单验证
					if($_POST['form_note'] == '')
					{
						$error = 1;
					} 
					else if((int)$_POST['check'] == 0) 
					{
						$error = 2;
					}
					if($error != 0)
					{
						$this->render('index',array(
							'data'=>$data,
							'error' => $error,
							'post' => $_POST,
						));
						Yii::app()->end();
					}

					//调动元素的属性、职级调整
					foreach ($data['transfer']->user as $key => $value) {
						if($_POST['attr_id-'.$value->id] !=0) {
							if ($_POST['change-'.$value->id] == 0) //如果为否
								$_POST['attr_id-'.$value->id] = $_POST['attr_L_id-'.$value->id] = $_POST['attr_M_id-'.$value->id] = 0;
							$model = new TransferUser;
							$m = $model->findByPk($value->id);
							$m->new_attr_id = $_POST['attr_id-'.$value->id];
							$m->new_L = $_POST['attr_L_id-'.$value->id];
							$m->new_M = $_POST['attr_M_id-'.$value->id];
							if($m->update()) {

							}
						}
					}
					//print_r($_POST);
					//die;
					$transferModel->updateIndex($data , (int)$_POST['check'] ,$_POST['form_note'], $this->userInfo->unique_id);
					$this->showAlert(array('type'=>'1','content'=>'审核成功！','url'=>'transfer/inbox'));
				}
				$positionLevelsM = HrPositionLevel::model()->findAllByAttributes(array('status' => 1),' Level like "M%"');
				$positionLevelOptionM[0] = '请选择';
				foreach ($positionLevelsM as $value) {
					$positionLevelOptionM[$value['id']] = $value['name'];
				}
				$pa =  HrPositionAttribute::model()->findAll();
				$paOption[0] = '请选择';
				foreach ($pa as $key => $value) {
					$paOption[$value['id']] = $value['title'];
				}

				$this->render('index',array(
					'data'=>$data,
					'error' => $error,
					'formType' => $formType,
					'positionLevelOptionL'=>$positionLevelOptionL,
					'positionLevelOptionM'=>$positionLevelOptionM,
					'paOption' => $paOption
				));
			}
		}
	}



	public function actionGetLByAttrId()
	{
		if($_GET['attr_id'])
		{
			$res = HrPositionLevel::model()->findAllByAttributes(array('attr_id'=>(int)$_GET['attr_id']));
			$tools = new Tools;
			if($res) 
				echo CJSON::encode($res);
			else 
				Tools::returnJson(1);
		}
	}

}
